package com.os.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.os.model.UserM;
import com.os.service.LoginServiceM;
import com.os.serviceimpl.LoginServiceImplM;

import net.sf.json.JSONObject;
/**
 * 
 * @author MWX
 *
 */
public class LoginControllerM extends HttpServlet{
	LoginServiceM lsm=new LoginServiceImplM();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doPost(req, resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    req.setCharacterEncoding("utf-8");
	    HttpSession session=req.getSession();
	    PrintWriter pw=resp.getWriter();
		String uname=req.getParameter("uname");
		String password=req.getParameter("upsd");
		session.setAttribute("uname", uname);
		session.setAttribute("upsd", password);
		UserM currentUser=lsm.isLogin(uname,password);
		int uid=currentUser.getUid();
		int rid=currentUser.getRid();
		session.setAttribute("uid", uid);
		session.setAttribute("rid", rid);
		JSONObject j=new JSONObject();
		if(currentUser.getUid()!=0){
		j.put("msg", 1);
		}else{
		j.put("msg", 0);
		}
		pw.write(j.toString());
	}
}
